import ACrypto from "@/utils/crypto";
import { useAppStore } from "@/stores/modules/app";
import http from "@/api"; //axios封装
import { useDynamicStore } from "@/stores/modules/dynamic";
import RuleClass from "../../ts/FieldRule";

const dynamicStore = useDynamicStore();

/**
 * 移交数据提交
 */
export const submit = async (data: any[], status: number, isOpen: boolean, tableName: string) => {
  let result: any[] = [];
  const appStore = useAppStore();
  const { LoadingConfig } = appStore;
  for (let index = 0; index < data.length; index++) {
    // 开启loading加载
    LoadingConfig.contentLoading = true;
    LoadingConfig.contentTip = `提交数据${index}/${data.length}`;
    const item = data[index];
    if (status !== 100) {
      item.Status = status;
    }
    delete item._X_ROW_KEY; //删除表格自带属性
    delete item.checkbox; //删除表格自带的属性
    const tempObj = { ...item, Open: isOpen };
    const FieldRuleClass = new RuleClass({
      modalFormData: tempObj,
      fieldName: "Status",
      ruleList: dynamicStore.visibleConfig.FieldRule.filter((v: any) => v.FR_Enable === true),
      tableName
    }); // 执行字段规则类方法
    if (status !== 100) {
      await FieldRuleClass.fieldRuleEvent2();
    }
    const param = {
      list: [{ ...tempObj }],
      tableName
    };
    const res = await http.put("Dynamic", param, { noLoading: true });
    if (status === 12 || status === 22) {
      const index2 = dynamicStore.tableData.findIndex((v: any) => v.Record_ID === item.Record_ID);
      if (index2 > -1) {
        dynamicStore.tableData.splice(index2, 1);
      }
    } else {
      const index2 = dynamicStore.tableData.findIndex((v: any) => v.Record_ID === item.Record_ID);
      if (index2 > -1) {
        dynamicStore.tableData.splice(index2, 1, res.List[0]);
      }
    }
  }
  LoadingConfig.contentLoading = false;
};
/**
 * 对移交数据标题进行解密
 */
export const formatter = ({ cellValue }: any) => {
  if (!cellValue) {
    return "";
  }
  const result = ACrypto.getSS(cellValue);
  return result ? result : "";
};
